package com.xy.dyson.service.impl;

import com.xy.dyson.dao.ResourcesMapper;
import com.xy.dyson.entity.Resources;
import com.xy.dyson.entity.SyntheticRelation;
import com.xy.dyson.service.ResourcesService;
import com.xy.dyson.service.SyntheticRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (Resources)表服务实现类
 *
 * @author XY
 * @since 2021-01-28 16:11:31
 */
@Service("resourcesService")
public class ResourcesServiceImpl implements ResourcesService {
    @Resource
    private ResourcesMapper resourcesMapper;

    @Autowired
    SyntheticRelationService relationService;

    /**
     * 新增或修改
     *
     * @return 实例对象
     */
    @Override
    public void saveOrUpdate(Resources resources) {

    }


    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Resources queryById(Integer id) {
        return this.resourcesMapper.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Resources> queryAllByLimit(int offset, int limit) {
        return this.resourcesMapper.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param resources 实例对象
     * @return 实例对象
     */
    @Override
    public Resources insert(Resources resources) {
        this.resourcesMapper.insert(resources);
        return this.queryAll(resources).get(0);
    }

    /**
     * 修改数据
     *
     * @param resources 实例对象
     * @return 实例对象
     */
    @Override
    public Resources update(Resources resources) {
        this.resourcesMapper.update(resources);
        return this.queryById(resources.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.resourcesMapper.deleteById(id) > 0;
    }

    @Cacheable(value = "dyson",key ="'resource::'+#id" )
    @Override
    public Resources queryTree(Integer id) {
        Resources resources = resourcesMapper.queryById(id);
        test(resources);
        return resources;
    }

    @Override
    public List<Resources> queryAll(Resources resources) {
        return resourcesMapper.queryAll(resources);
    }


    private void test(Resources resources) {
        SyntheticRelation syntheticRelation = new SyntheticRelation();
        syntheticRelation.setResourceId(resources.getId());
        List<SyntheticRelation> syntheticRelations = relationService.queryAll(syntheticRelation);
        if(syntheticRelations!=null&&syntheticRelations.size()>0){
            List<Resources> collect = syntheticRelations.stream().map(item -> {
                Resources resources1 = new Resources();
                Resources aa = resourcesMapper.queryById(item.getRequireResourceId());
                return aa;
            }).collect(Collectors.toList());
            if(collect!=null&&collect.size()>0){
                resources.setChildren(collect);
                for (Resources resources1 : collect) {
                    test(resources1);
                }
            }

        }
    }
}